package top.aicats.blog.BlogManage.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.aicats.blog.BlogManage.model.*;
import top.aicats.blog.BlogManage.service.IMainBlogTypeService;
import top.aicats.blog.BlogManage.service.IMainSubTypeService;
import top.aicats.blog.BlogManage.service.ISubBlogTypeService;

import java.util.List;

/**
 * @author sjc
 * @version 1.0
 * @date 2020/1/7 21:01
 * @email 952491025@qq.com
 */

@RestController()
@RequestMapping(value = "/blogType")
public class BlogTypeManageControl {

    @Autowired
    IMainBlogTypeService mainBlogTypeService;

    @Autowired
    ISubBlogTypeService subBlogTypeService;

    @Autowired
    IMainSubTypeService mainSubTypeService;

    @PostMapping("/addMainType")
    @ResponseBody
    public ResultObj AddMainType(BlogMainType type) {
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            int result = mainBlogTypeService.addOneMainBlogType(type);
            if (result > 0) {
                resultObj.setErrorCode(ErrorCode.OK);
                resultObj.setErrorContent("添加成功");
            }
            return resultObj;
        } catch (Exception ex) {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setErrorContent("添加主博客类型失败" + ex.toString());
            return resultObj;
        }
    }

    @GetMapping("/getMainTypes")
    @ResponseBody
    public ResultObj getMainTypes() {
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            List<BlogMainType> types = mainBlogTypeService.getAllMainBlogType();
            if (types.size() > 0) {
                resultObj.setErrorCode(ErrorCode.OK);
                resultObj.setData(types);
            }
            resultObj.setErrorContent("添加成功");
            return resultObj;
        } catch (Exception ex) {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setErrorContent("添加子博客类型失败");
            return resultObj;
        }
    }


    @PostMapping("/deleteMainType")
    @ResponseBody
    public ResultObj deleteMainType(BlogMainType type) {
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            int iDeleteCount = mainBlogTypeService.deleteMainBlogType(type);
            resultObj.setErrorCode(ErrorCode.OK);
            resultObj.setData("删除成功");

            return resultObj;
        } catch (Exception ex) {
            resultObj.setData(ErrorCode.ERROR);
            resultObj.setErrorContent(ex.toString());
            resultObj.setData("删除失败");
            return resultObj;
        }
    }

    @PostMapping("/addSubType")
    @ResponseBody
    public ResultObj AddSubType(BlogSubType type) {
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            int result = subBlogTypeService.addOneSubBlogType(type);
            if (result > 0) {
                resultObj.setErrorCode(ErrorCode.OK);
                resultObj.setErrorContent("添加成功");
            }
            return resultObj;
        } catch (Exception ex) {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setErrorContent("添加子博客类型失败");
            return resultObj;
        }
    }

    @GetMapping("/getSubTypes")
    @ResponseBody
    public ResultObj getSubTypes(String mainBlogRefid) {
        BlogSubType blogSubType = new BlogSubType();
        blogSubType.setBlogSubTypeForeign(mainBlogRefid);
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            List<BlogSubType> result = subBlogTypeService.getSubBlogTypeByMainRefid(blogSubType);
            if (result.size() > 0) {
                resultObj.setErrorCode(ErrorCode.OK);
                resultObj.setData(result);
                resultObj.setErrorContent("添加成功");
            }
            return resultObj;
        } catch (Exception ex) {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setErrorContent("添加子博客类型失败");
            return resultObj;
        }
    }

    @GetMapping("/getMainSubTypes")
    @ResponseBody
    public ResultObj getAllMainSubBlogTypes() {
        ResultObj resultObj = new ResultObj();
        try {
            List<BlogMainSubType> blogMainSubTypeList = mainSubTypeService.getAllMainSubTypes();
            resultObj.setErrorCode(ErrorCode.OK);
            resultObj.setData(blogMainSubTypeList);
        } catch (Exception ex) {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setErrorContent(ex.toString());
        }
        return resultObj;
    }


    @PostMapping("/deleteSubType")
    @ResponseBody
    public ResultObj deleteSubType(BlogSubType type) {
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            subBlogTypeService.deleteSubType(type);
            resultObj.setErrorCode(ErrorCode.OK);
            resultObj.setData("删除成功");
            return resultObj;
        } catch (Exception ex) {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setData("删除失败");
            return resultObj;
        }
    }

}
